package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bean.Info;
import com.example.bean.blockchainCompanyInfo;
import com.example.bean.sysUser;
import com.example.common.BaseContext;
import com.example.common.R;
import com.example.service.InfoService;
import com.example.service.UserService;
import com.example.service.BlockchainCompanyInfoService;
import com.example.utils.PDFUtils;
import com.example.utils.PathUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    BlockchainCompanyInfoService blockchainCompanyInfoService;
    @Autowired
    UserService userService;

    @Autowired
    PathUtils pathUtils;

    @Autowired
    PDFUtils pdfUtils;

    @Autowired
    InfoService infoService;

    @PostMapping("/login")
    public R login(HttpServletRequest request, @RequestParam String username, String password){


        LambdaQueryWrapper<sysUser> wrapper = new LambdaQueryWrapper<sysUser>();
        wrapper.eq(sysUser::getUsername,username);
        wrapper.eq(sysUser::getPassword,password);

        sysUser one = userService.getOne(wrapper);

        if(one != null){
            BaseContext.setThreadLocal((long) one.getId());
            request.getSession().setAttribute("User",BaseContext.getThreadLocal());
            return R.success("登录成功！");

        }else{
            return R.error("用户名或密码错误！");
        }
    }

    @PostMapping("/register")
    public R register(@RequestBody sysUser user){

        user.setCreatedBy(user.getUsername());

        userService.save(user);

        return R.success("注册成功！");
    }

    @GetMapping("/forget")
    public R change(String email){

        LambdaQueryWrapper<sysUser> wrapper = new LambdaQueryWrapper<sysUser>();

        System.out.println(email);
        wrapper.eq(sysUser::getEmail,email);

        sysUser one = userService.getOne(wrapper);

        System.out.println(one);
        if(one!=null){
            one.setPassword("123456@W");
            userService.updateById(one);
            return R.success("修改成功！");
        }
        return R.error("修改失败！");
    }

    @PostMapping("/commit")
    public R commit(@RequestBody Info info){

        info.setComName("区块链总部");
        infoService.save(info);

        return R.success("提交成功！");

    }

    @GetMapping("/list")
    public R<Page> list(int page,int pageSize,String name){

        Page pg = new Page(page, pageSize);

        LambdaQueryWrapper<blockchainCompanyInfo> wrapper = new LambdaQueryWrapper<blockchainCompanyInfo>();

        wrapper.like(StringUtils.isNotEmpty(name),blockchainCompanyInfo::getName,name);


        blockchainCompanyInfoService.page(pg, wrapper);

        return R.success(pg);

    }

    @GetMapping("/submit")
    public R<Page> submit(int page,int pageSize,String name){

        Page pg = new Page(page, pageSize);

        LambdaQueryWrapper<Info> wrapper = new LambdaQueryWrapper<Info>();

        wrapper.like(StringUtils.isNotEmpty(name),Info::getName,name);

        infoService.page(pg, wrapper);

        return R.success(pg);

    }

    @DeleteMapping("/delete")
    public R delete(int id){

        blockchainCompanyInfoService.removeById(id);

        return R.success("删除成功！");
    }

//    @GetMapping("/download")
//    public R download(int id){
//
//        LambdaQueryWrapper<blockchainCompanyInfo> wrapper = new LambdaQueryWrapper<blockchainCompanyInfo>();
//        wrapper.eq(blockchainCompanyInfo::getId,id);
//
//        blockchainCompanyInfo one = blockchainCompanyInfoService.getOne(wrapper);
//
//        Map<String,Map> m = new HashMap<>();
//        Map<String,String> m1 = new HashMap<>();
//
//        m1.put("name","1111");
//        m1.put("企业类型","22222");
//        m.put("data",m1);
//
//        System.out.println(m.get("data"));
//
//        ByteArrayOutputStream byteArrayOutputStream =
//                pathUtils.generatePdf(m, "F:\\IDEA\\Springboot2\\src\\main\\resources\\static\\download.pdf");
//
//        pathUtils.extracted(byteArrayOutputStream);
//        return R.success("下载成功！");
//    }

    @GetMapping("/download")
    public R download(int id){

        LambdaQueryWrapper<blockchainCompanyInfo> wrapper = new LambdaQueryWrapper<blockchainCompanyInfo>();
        wrapper.eq(blockchainCompanyInfo::getId,id);

        blockchainCompanyInfo one = blockchainCompanyInfoService.getOne(wrapper);

        Map<String,String> m = new HashMap<>();

        m.put("f1",one.getName());
        m.put("f2",one.getGeneralCreditCode());
        m.put("f3",one.getEnterpriseType());
        m.put("f4",one.getCompanyType());
        m.put("f5",String.valueOf(one.getCreatedDate()));

        pdfUtils.test(m);

        return R.success("下载成功！");
    }

    @GetMapping("/email")
    public R email(){

        Random random = new Random();

        int i = random.nextInt(10);
        int b = random.nextInt(10);
        int c = random.nextInt(10);
        int d = random.nextInt(10);
        String s = ""+i+b+c+d;

        return R.success(s);
    }







}
